import express from 'express'
import bodyParser  from 'body-parser'
import cors from 'cors'

import { login, logout } from './controller/auth.js'
import { getCurrentUserMenuList } from './controller/menu.js'
import { getUserList } from './controller/user.js'

// var express = require('express')


var app = express()
// 解析 application/json
app.use(bodyParser.json());
// 解析 application/x-www-form-urlencoded
app.use(bodyParser.urlencoded());

// 设置跨域访问
app.all('*', function(req, res, next) {
	res.header('Access-Control-Allow-Origin', '*')
	res.header('Access-Control-Allow-Headers', 'X-Requested-With')
	res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
	res.header('X-Powered-By', ' 3.2.1')
	res.header('Content-Type', 'application/json;charset=utf-8')
	next()
})

app.use(
	cors({
		origin: ['http://localhost:3333'],
		methods: ['get', 'post'],
		allowedHeaders: ['Content-Type', 'Authorization']
	})
)

// 登录
app.post('/login', login)

// 退出
app.post('/logout', logout)

// 返回当前用户有权限的所有菜单列表
app.get('/sys/menu/currentlist', getCurrentUserMenuList)

// 返回用户列表
app.post('/sys/user/list', getUserList)

app.use(function(req, res, next) {
	console.log(req.url, req.method, req.get('125'), req.body)
	next()
});

//服务端监听地址
var server = app.listen(3333, function() {
	var host = server.address().address
	var port = server.address().port
	console.log(host, port)
})
